<template>
  <el-dialog v-if="load" title="导出销量" v-model="visible" append-to-body :close-on-click-modal="false" :z-index="2000" width="500px" @close="handleClose">
    <el-radio-group v-model="form.export_type">
      <el-radio :value="1">30天销量</el-radio>
      <el-radio :value="2">90天销量</el-radio>
    </el-radio-group>
    <el-alert :closable="false">注：导出完成后文件会发送到飞书账户</el-alert>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">导 出</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { createName } from "@/utils"

const props = defineProps({
  options: { type: Object, default: () => ({}) },
  tableRef: { type: Object, default: () => ({}) }, // 列表表格对象
})
const emit = defineEmits(['close', 'success'])

const api = inject('api')
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const type = inject('type')

// 打开弹窗
const open = async () => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value.export_type = 1
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = () => {
  loadings.submit = true
  const condition = { is_ids: props.tableRef.$table.getCheckboxRecords().map(d => d.is_id), ...props.tableRef.getQueryForm(), ...form.value, type }
  api.exportSale(condition).then(blob => {
    ElMessage.success('导出完成后文件会发送到飞书账户')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>